package com.isunland.managesystem.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.GuideMenu;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/14 10:41
 */
public class GridViewAdapter extends BaseAdapter {
  private Context context;
  private ArrayList<GuideMenu> mList;
  private ImageLoader mImageLoader;

  public GridViewAdapter(Context context, ArrayList<GuideMenu> list) {
    this.context = context;
    this.mList = list;
    this.mImageLoader = RequestManager.getImageLoader();
  }

  @Override public int getCount() {
    return mList.size();
  }

  @Override public Object getItem(int i) {
    return mList.get(i);
  }

  @Override public long getItemId(int i) {
    return i;
  }

  @TargetApi(Build.VERSION_CODES.M) @Override
  public View getView(int i, View view, ViewGroup viewGroup) {
    if (view == null) {
      view = LayoutInflater.from(context).inflate(R.layout.gridview_item, null);
    }
    GuideMenu item = mList.get(i);

    LinearLayout llGuideName = (LinearLayout) view.findViewById(R.id.ll_item);
    TextView tvGuideName = (TextView) view.findViewById(R.id.tv_guideName);
    ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);

    tvGuideName.setText(item.getResName());
    //        tvGuideName.setBackgroundResource(R.color.lightcoral);
    //动态创建圆角
    GradientDrawable gd = new GradientDrawable();//创建drawable
    gd.setColor(context.getResources().getColor(R.color.white));//内部填充颜色
    gd.setCornerRadius(0);//圆角半径
    gd.setStroke(2, context.getResources().getColor(R.color.grid_stroke));//边框宽度,颜色
    llGuideName.setBackgroundDrawable(gd);

    // 图标
    String icon = item.getDefaultUrl();

    if (!TextUtils.isEmpty(icon)) {
      String urlImage = ApiConst.getAbsoluteUrl(icon);
      ImageLoader.ImageListener listener = ImageLoader.getImageListener(ivIcon, // 控件名
          R.drawable.icon_liuchengrenwu, R.drawable.icon_liuchengrenwu); // 默认图标
      mImageLoader.get(urlImage, listener);
    } else {
      ivIcon.setImageResource(R.drawable.icon_liuchengrenwu);
    }
    return view;
  }
}
